package com.example.restaurantclient.Activity.Shop;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.restaurantclient.ConfigUtil.ConfigUtil;
import com.example.restaurantclient.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;



public class ConfirmActivity extends AppCompatActivity {
    private ImageView imageView;
    private TextView name;
    private TextView price;
    private TextView prices;
    private Button button;

    private EditText buyer;
    private EditText tel;
    private EditText add;
    private EditText rem;

    private int id;
    private String pname;
    private int pintegral;
    private String buy;
    private String tell;
    private String address;
    private String remark;
    private int amount;
    private String picture;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm);
        imageView = findViewById(R.id.img_goods);
        name = findViewById(R.id.tv_commodity);
        price = findViewById(R.id.tv_scoring);
        prices = findViewById(R.id.list_item_sum_price);
        button = findViewById(R.id.list_item_qujiesuan);

        buyer = findViewById(R.id.ed_name);
        tel = findViewById(R.id.ed_tel);
        add = findViewById(R.id.ed_address);
        rem = findViewById(R.id.ed_remark);

        Intent intent = getIntent();
        id = intent.getIntExtra("id",0);
        pname = intent.getStringExtra("name");
        pintegral = intent.getIntExtra("integral",0);
        amount = intent.getIntExtra("amount",0);
        picture = intent.getStringExtra("picture");

        name.setText(pname);
        price.setText(pintegral+"");
        prices.setText(pintegral+"");

        Glide.with(this)
                .load(picture)
                .into(imageView);

//        button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                buy = buyer.getText().toString();
//                tell = tel.getText().toString();
//                address = add.getText().toString();
//                remark = rem.getText().toString();
//                if (Variable.loginIdentity == 1){
//                    int point = Integer.parseInt(Variable.teacher.getPoints());
//                    if(point >= pintegral && amount > 0){
//                        new Thread(){
//                            @Override
//                            public void run() {
//                                super.run();
//                                try {
//                                    URL url = new URL(ConfigUtil.SERVER_ADDR+"AddOrderTeacherServlet");
//                                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//                                    //设置Http的请求方式：get、post、put、delete...,默认是get请求
//                                    connection.setRequestMethod("POST");
//                                    OutputStream outputStream = connection.getOutputStream();
//                                    JSONObject object=new JSONObject();
//                                    object.put("goods_name",pname);
//                                    object.put("goods_integral",pintegral);
//                                    object.put("pname",buy);
//                                    object.put("ptel",tell);
//                                    object.put("paddress",address);
//                                    object.put("prem",remark);
//                                    object.put("amount",amount);
//                                    object.put("goodsID",id);
//                                    object.put("picture",picture);
//                                    object.put("teacherID",Variable.teacher.getId());
//                                    object.put("teacherIntegral",Variable.teacher.getPoints());
//                                    outputStream.write(object.toString().getBytes());
//
//                                    InputStream in = connection.getInputStream();
//                                    BufferedReader reader = new BufferedReader(new InputStreamReader(in,"utf-8"));
//                                    if(reader.readLine().equals("true")) {
//                                        Looper.prepare();
//                                        Toast.makeText(ConfirmActivity.this,"兑换成功", Toast.LENGTH_SHORT).show();
//                                        int i = Integer.parseInt(Variable.teacher.getPoints());
//                                        int i1 = i-pintegral;
//                                        Variable.teacher.setPoints(i1+"");
//                                        Looper.loop();
//                                    }
//                                    in.close();
//                                    outputStream.close();
//                                } catch (MalformedURLException e) {
//                                    e.printStackTrace();
//                                } catch (UnsupportedEncodingException e) {
//                                    e.printStackTrace();
//                                } catch (ProtocolException e) {
//                                    e.printStackTrace();
//                                } catch (IOException e) {
//                                    e.printStackTrace();
//                                } catch (JSONException e) {
//                                    e.printStackTrace();
//                                }
//
//                            }
//                        }.start();
//                    }else {
//                        Toast.makeText(ConfirmActivity.this,"兑换失败", Toast.LENGTH_SHORT).show();
//                    }
//                }else {
//                    int point = Integer.parseInt(Variable.parent.getPoints());
//                    if (point >= pintegral && amount > 0){
//                        new Thread(){
//                            @Override
//                            public void run() {
//                                super.run();
//                                try {
//                                    Log.e("111",Variable.parent.getId());
////                                    int parentID = Integer.parseInt(Variable.parent.getId());
//                                    URL url = new URL(ConfigUtil.SERVER_ADDR+"AddParentOrderServlet");
//                                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//                                    //设置Http的请求方式：get、post、put、delete...,默认是get请求
//                                    connection.setRequestMethod("POST");
//                                    OutputStream outputStream = connection.getOutputStream();
//                                    JSONObject object=new JSONObject();
//                                    object.put("goods_name",pname);
//                                    object.put("goods_integral",pintegral);
//                                    object.put("pname",buy);
//                                    object.put("ptel",tell);
//                                    object.put("paddress",address);
//                                    object.put("prem",remark);
//                                    object.put("amount",amount);
//                                    object.put("goodsID",id);
//                                    object.put("picture",picture);
//                                    object.put("parentID",Variable.parent.getId());
//                                    object.put("parentIntegral",Variable.parent.getPoints());
//                                    outputStream.write(object.toString().getBytes());
//
//                                    InputStream in = connection.getInputStream();
//                                    BufferedReader reader = new BufferedReader(new InputStreamReader(in,"utf-8"));
//                                    if(reader.readLine().equals("true")) {
//                                        Looper.prepare();
//                                        Toast.makeText(ConfirmActivity.this,"兑换成功", Toast.LENGTH_SHORT).show();
//                                        int i = Integer.parseInt(Variable.parent.getPoints());
//                                        int i1 = i-pintegral;
//                                        Variable.parent.setPoints(i1+"");
//                                        Looper.loop();
//                                    }
//                                    in.close();
//                                    outputStream.close();
//                                } catch (MalformedURLException e) {
//                                    e.printStackTrace();
//                                } catch (UnsupportedEncodingException e) {
//                                    e.printStackTrace();
//                                } catch (IOException e) {
//                                    e.printStackTrace();
//                                } catch (JSONException e) {
//                                    e.printStackTrace();
//                                }
//                            }
//                        }.start();
//                    }else {
//                        Toast.makeText(ConfirmActivity.this,"兑换失败", Toast.LENGTH_SHORT).show();
//                    }
//                }
////                Intent intent1=new Intent(ConfirmActivity.this,ShopActivity.class);
////                startActivity(intent1);
//                finish();
//            }
//        });
    }
}
